package com.a0soft.gphone.aTruffleHog.wnd.phone;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *   Alger Lin <addre75 (at) gmail.com>
 *
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.analytic.AnalyticsUtil;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseSinglePaneActivity;
import com.a0soft.gphone.aTruffleHog.scrsht.Screenshot;
import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.a0soft.gphone.aTruffleHog.wnd.InfoFrg;
import com.a0soft.gphone.aTruffleHog.wnd.ProgressDlgFrg;
import com.a0soft.gphone.aTruffleHog.wnd.info.SendDevInfoByMail;

public class InfoWnd extends BaseSinglePaneActivity {
    public static final String EXTRA_FOR_PREVIEW = InfoWnd.class.getName() + ".fp";

    private DialogFragment m_frgDlg;
    
	private static final String LOG_TAG = InfoWnd.class.getSimpleName();

    //////////////////////////////////////////// 
    @Override protected void 
    onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        if (!DeviceInfo.GetInstance(this).IsReady()) {
            finish();
            return;
        }
    }
	
    @Override protected Fragment 
    onCreatePane() 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreatePane()");
        return new InfoFrg();
    }

    @Override protected void 
    onPostCreate(Bundle bdSavedInstanceState) 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onPostCreate()");
        super.onPostCreate(bdSavedInstanceState);
        
        getActivityHelper().setupSubActivity();
        
        AnalyticsUtil.GetInstance(this).TrackPageView(AnalyticsUtil.PAGE_DEVICE_INFO);
    }

    @Override public void 
    onNotifyByFragment(Class<?> clsFg, Intent it)
    {
        if (aDebug.ENABLED) {
            aDebug.d(LOG_TAG, "onNotifyByFragment()");
        }
        return;
        /*
        if (it == null || !clsFg.equals(StatsListFrg.class)) return;
        
        int nSelItemPos = it.getIntExtra(StatsListFrg.EXTRA_SEL_ITEM_POS, Integer.MIN_VALUE);
        boolean bSelItemChanged = it.getBooleanExtra(StatsListFrg.EXTRA_SEL_ITEM_CHANGED, false);    
        boolean bSelChangedManually = it.getBooleanExtra(StatsListFrg.EXTRA_SEL_CHANGED_MANUALLY, true);
        if (aDebug.ENABLED) {
            aDebug.d(LOG_TAG, "  nSelItemPos=" + nSelItemPos);
            aDebug.d(LOG_TAG, "  bSelItemChanged=" + bSelItemChanged);
            aDebug.d(LOG_TAG, "  bSelChangedManually=" + bSelChangedManually);
        }
        
        if (!bSelChangedManually) return;
        
        switch (nSelItemPos) {
        case StatsListFrg.ITEM_OS_STATS:
            it.setClass(this, StatDetailWnd.class);
            startActivity(it);
            break;
        }
        */
    }
    
    @Override public boolean
    onCreateOptionsMenu(Menu menu) 
    {
        if (aDebug.ENABLED) 
            aDebug.d(LOG_TAG, "onCreateOptionsMenu()");
        getMenuInflater().inflate(R.menu.info_wnd, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override public boolean 
    onOptionsItemSelected(MenuItem item)
    {
        if (aDebug.ENABLED) 
            aDebug.d(LOG_TAG, "onOptionsItemSelected()");
        switch(item.getItemId()) {
            case R.id.menu_mail:
                SendDevInfoByMail.Send(this);
                return true;
            case R.id.menu_screenshot:
                Screenshot.TakeScreenshot(this);
                return true;
            case R.id.menu_refresh:
                _Refresh();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override protected void 
    onResume()
    {
        super.onResume();
        
        if (m_frgDlg != null) {
            if (m_frgDlg.getFragmentManager() != null)
                m_frgDlg.dismiss();
            m_frgDlg = null;
        }
    }
    
    private void
    _Refresh()
    {
        final FragmentManager mgrFrg = getSupportFragmentManager();
        if (mgrFrg == null)
            return;
        final InfoFrg frgInfo = (InfoFrg)GetFragment();
        if (frgInfo != null) {
            final String strTag = ProgressDlgFrg.class.getSimpleName();
            m_frgDlg = (DialogFragment)getSupportFragmentManager().findFragmentByTag(strTag);
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "m_frgDlg found:" + (m_frgDlg != null));

            if (m_frgDlg == null) {
                m_frgDlg = ProgressDlgFrg.NewInstance(0, R.string.device_info_building);
                m_frgDlg.show(getSupportFragmentManager(), strTag);
            }
            
            DeviceInfo di = DeviceInfo.GetInstance(this);
            di.Init(true, new DeviceInfo.OnListener() {            
                @Override public void 
                OnDeviceInfoReady(DeviceInfo di)
                {
                    if (aDebug.ENABLED)
                        aDebug.d(LOG_TAG, "OnDeviceInfoReady()");
                    
                    if (m_frgDlg != null) {
                        if (m_frgDlg.getFragmentManager() != null && 
                            InfoWnd.this.IsVisible())
                            m_frgDlg.dismiss();
                        m_frgDlg = null;
                    }

                    frgInfo.Refresh();
                }
            });
        }
    }
}
